import { Callout } from "nextra/components";

# Go SDK

This is the Hatchet Go SDK reference. On this page, we'll get you up and running with a Go worker. This guide assumes that you already have a Hatchet engine instance running. If you don't, you can:

- Sign up on [Hatchet Cloud](https://cloud.onhatchet.run)
- [Self-host Hatchet](https://docs.hatchet.run/self-hosting)

<Callout type="info" emoji="🪓">
  If you run into any issues, please file an issue on the [Hatchet GitHub
  repository](https://github.com/hatchet-dev/hatchet).
</Callout>

## Installation

Run the following to install the Hatchet Go SDK:

```sh
go get github.com/hatchet-dev/hatchet/pkg
```

## Generate a Token

Navigate to your Hatchet dashboard and navigate to your settings tab. You should see a section called "API Keys". Click "Create API Key", input a name for the key and copy the key. Then set the following environment variables:

```sh
HATCHET_CLIENT_TOKEN="<your-api-key>"
```

<Callout type="info" emoji="🪓">
You may need to set additional environment variables depending on your self-hosted configuration. The Hatchet clients default to SSL by default, but to disable this you can set:

```
HATCHET_CLIENT_TLS_STRATEGY=none
```

</Callout>

## Run your first worker

Create a `main.go` file with the following contents:

```go filename="main.go" copy
package main

import (
	"fmt"

	"github.com/hatchet-dev/hatchet/pkg/client"
	"github.com/hatchet-dev/hatchet/pkg/cmdutils"
	"github.com/hatchet-dev/hatchet/pkg/worker"
)

type stepOutput struct{}

func main() {
	c, err := client.New()

	if err != nil {
		panic(fmt.Sprintf("error creating client: %v", err))
	}

	w, err := worker.NewWorker(
		worker.WithClient(
			c,
		),
		worker.WithMaxRuns(1),
	)

	if err != nil {
		panic(fmt.Sprintf("error creating worker: %v", err))
	}

	err = w.RegisterWorkflow(
		&worker.WorkflowJob{
			Name:        "simple-workflow",
			Description: "Simple one-step workflow.",
			On:          worker.Events("simple"),
			Steps: []*worker.WorkflowStep{
				worker.Fn(func(ctx worker.HatchetContext) (result *stepOutput, err error) {
					fmt.Println("executed step 1")

					return &stepOutput{}, nil
				},
				).SetName("step-one"),
			},
		},
	)

	if err != nil {
		panic(fmt.Sprintf("error registering workflow: %v", err))
	}

	interruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan())
	defer cancel()

	cleanup, err := w.Start()
	if err != nil {
		panic(fmt.Sprintf("error starting worker: %v", err))
	}

	<-interruptCtx.Done()
	if err := cleanup(); err != nil {
		panic(err)
	}
}
```

Next, run the following command to start the worker:

```sh
go run main.go
```

## Run your first workflow

The worker is now running and listening for steps to execute. You should see your first worker registered in the `Workers` tab of the Hatchet dashboard:

![Quickstart 1](/quickstart-1.png)

You can now trigger your first workflow by navigating to the `Workflows` tab, selecting your workflow, and clicking the top right "Trigger workflow" button:

![Quickstart 2](/quickstart-2.png)

That's it! You've successfully deployed Hatchet and run your first workflow.

## Next Steps

Congratulations on running your first workflow!

To test out more complex workflows, check out the [Hatchet Go Quickstart](https://github.com/hatchet-dev/hatchet-go-quickstart).
